package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/7 23:05
 */
@RestController
@RequestMapping("/admin/product")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private SpuImageService spuImageService;
    @Autowired
    private SpuSaleAttrService spuSaleAttrService;
    /**
     * spu信息的分页查询（带条件）
     *
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable("page") Integer page,
                                 @PathVariable("limit") Integer limit,
                                 @RequestParam("category3Id") Long category3Id) {
        Page<SpuInfo> iPage = new Page(page, limit);
        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id", category3Id);
        Page<SpuInfo> page1 = spuInfoService.page(iPage, wrapper);
        return Result.ok(page1);
    }


    /**
     * spu信息的添加
     *
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        spuInfoService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 根据spuId获取图片列表
     * http://192.168.200.1/admin/product/spuImageList/24
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result saveImageList(@PathVariable Long spuId) {
        QueryWrapper<SpuImage> wrapper = new QueryWrapper();
        wrapper.eq("spu_id",spuId);
        List<SpuImage> list = spuImageService.list(wrapper);
        return Result.ok(list);
    }

    /**
     * 根据spuId获取销售属性
     * http://192.168.200.1/admin/product/spuSaleAttrList/24
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
           List<SpuSaleAttr> list = spuSaleAttrService.getSpuSaleAttrsBySpuId(spuId);
           return Result.ok(list);
    }


}
